package org.jeecg.modules.demo.crm.service.impl;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.crm.entity.CProjectApplication;
import org.jeecg.modules.demo.crm.entity.CProjectStatusLog;
import org.jeecg.modules.demo.crm.entity.CSubProject;
import org.jeecg.modules.demo.crm.entity.CSubProjectStatusLog;
import org.jeecg.modules.demo.crm.mapper.CProjectApplicationMapper;
import org.jeecg.modules.demo.crm.mapper.CProjectStatusLogMapper;
import org.jeecg.modules.demo.crm.mapper.CSubProjectMapper;
import org.jeecg.modules.demo.crm.service.ICProjectStatusLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;

/**
 * @Description: 项目状态变更表
 * @Author: jeecg-boot
 * @Date:   2025-05-27
 * @Version: V1.0
 */
@Service
public class CProjectStatusLogServiceImpl extends ServiceImpl<CProjectStatusLogMapper, CProjectStatusLog> implements ICProjectStatusLogService {


    @Autowired
    private CProjectStatusLogMapper cProjectStatusLogMapper;

    @Autowired
    private CProjectApplicationMapper cProjectApplicationMapper;


    @Override
    public void updateStatus(String id, String status) {
        updateStatus(id, status, null);
    }

    @Override
    public void updateStatus(String id, String status, String remark) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        String oldStatus = "";
        CProjectApplication cProjectApplication = cProjectApplicationMapper.selectById(id);
        if(cProjectApplication != null){
            oldStatus = cProjectApplication.getProjectStatus();
        }
        CProjectStatusLog cProjectStatusLog = new CProjectStatusLog();
        cProjectStatusLog.setProjectId(cProjectApplication.getId());
        cProjectStatusLog.setNewStatus(status);
        cProjectStatusLog.setOldStatus(oldStatus);
        cProjectStatusLog.setChangedBy(sysUser.getId());
        cProjectStatusLog.setRemark(remark);
        cProjectStatusLog.setChangeTime(new Date());
        cProjectStatusLogMapper.insert(cProjectStatusLog);
    }

    @Override
    public void add(CProjectStatusLog cProjectStatusLogDto) {
        CProjectApplication cProjectApplication = cProjectApplicationMapper.selectById(cProjectStatusLogDto.getProjectId());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();


        CProjectStatusLog cProjectStatusLog = new CProjectStatusLog();
        cProjectStatusLog.setProjectId(cProjectApplication.getId());
        cProjectStatusLog.setNewStatus(cProjectStatusLogDto.getNewStatus());
        cProjectStatusLog.setOldStatus(cProjectApplication.getProjectStatus());
        cProjectStatusLog.setChangedBy(sysUser.getId());
        cProjectStatusLog.setRemark(cProjectStatusLogDto.getRemark());
        cProjectStatusLog.setChangeTime(cProjectStatusLogDto.getChangeTime());
        cProjectStatusLogMapper.insert(cProjectStatusLog);
        cProjectApplication.setProjectStatus(cProjectStatusLogDto.getNewStatus());
        if("立项".equals(cProjectStatusLogDto.getNewStatus())){
            cProjectApplication.setEstablishmentTime(cProjectStatusLogDto.getChangeTime());
        }

        cProjectApplicationMapper.updateById(cProjectApplication);

    }
}
